package org.stvd.service.common.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.admin.Params;
import org.stvd.entities.admin.Roles;
import org.stvd.repository.admin.ParamsDao;
import org.stvd.repository.admin.RolesDao;
import org.stvd.service.common.CommonService;

/**
 * @Title: CommonServiceImpl
 * @Description: 公共方法业务层接口实现类
 * @author houzx
 * @date 2020年8月8日
 */
@Service("commonService")
public class CommonServiceImpl implements CommonService{

    @Resource(name = "RolesDao")
    private RolesDao roleDao;
    @Resource(name = "ParamsDao")
    private ParamsDao paramsDao;
    
    @Override
    public boolean checkManage(String userId, String guid) {
        List<Roles> rolesList = roleDao.findRolesByUserId(userId, guid);
        if(rolesList!=null && rolesList.size() > 0) {
            String rolesNameStr = "";
            String manageRoleCode = "";
            for (Roles roles : rolesList) {
                if(!StringUtil.isEmpty(rolesNameStr)) {
                    rolesNameStr += ",";
                }
                rolesNameStr += roles.getRoleCode();
            }
            if(!StringUtil.isEmpty(rolesNameStr)) {
                List<Params> paramsList = paramsDao.findByParamName("", "MANAGE_DEFAULT_ROLE");
                if(paramsList!=null &&paramsList.size() > 0) {
                    manageRoleCode = paramsList.get(0).getParamValue();
                }
                if(!StringUtil.isEmpty(manageRoleCode) 
                    && rolesNameStr.contains(manageRoleCode)) {
                    return true;
                }
            }
        }
        return false;
    }
}
